package com.lqq.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.lqq.dao.TopicDAO;
import com.lqq.entity.Topic;

public class TopicDAOImpl implements TopicDAO {

	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Topic> getAllTopic() {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Topic.class);
		criteria.addOrder(Order.desc("id"));
		
		return criteria.list();
	}

	@Override
	public Integer getAllCountOfTopic() {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Topic.class);
		
		return criteria.list().size();
	}

	@Override
	public Topic getTopicById(Integer id) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		return (Topic) session.get(Topic.class, id);
	}

	@Override
	public boolean saveTopic(Topic topic) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		try {
			session.save(topic);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean updateTopic(Topic topic) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		try {
			session.update(topic);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean deleteTopic(Topic topic) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		try {
			session.delete(topic);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Integer getAllCountOfTopicByCondition(Topic condition) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Topic.class);
		if(condition != null){
			if(condition.getName() != null && !("").equals(condition.getName())){
				criteria.add(Restrictions.like("name", condition.getName(),MatchMode.ANYWHERE));
			}
		}
		return criteria.list().size();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Topic> getTopicsByPage(int curPage, int pageSize) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Topic.class);
		criteria.setFirstResult((curPage - 1)*pageSize);
		criteria.setMaxResults(pageSize);
		criteria.addOrder(Order.desc("id"));
		
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Topic> getTopicsByPageAndConditon(Topic condition, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Topic.class);
		
		if(condition != null){
			if(condition.getName() != null &&("").equals(condition.getName())){
				criteria.add(Restrictions.like("name", condition.getName(),MatchMode.ANYWHERE));
			}
		}
		
		criteria.setFirstResult((curPage - 1)*pageSize);
		criteria.setMaxResults(pageSize);
		criteria.addOrder(Order.desc("id"));
		return criteria.list();
	}

}
